home *** CD-ROM | disk | FTP | other *** search
- #include<exec/libraries.h>
- #include<intuition/intuition.h>
- #include<utility/tagitem.h>
- #include<graphics/text.h>
- #include<graphics/rastport.h>
- #include<intuition/screens.h>
- #include<libraries/gadtools.h>
-
- #include<string.h>
- #include<stdio.h>
-
- #include<clib/exec_protos.h>
- #include<clib/graphics_protos.h>
- #include<clib/intuition_protos.h>
- #include<clib/gadtools_protos.h>
-
- /* The library base global variables */
- /* (The different style of opening libraries requires these to be initialised to NULL) */
- struct Library* GfxBase = NULL;
- struct Library* IntuitionBase = NULL;
- struct Library* GadToolsBase = NULL;
-
- /* The global handle on the palette gadget */
- struct Gadget* palgad = NULL;
- /* Global record of foreground pen */
- UBYTE pen;
- /* Global records of our windows */
- struct Window* drawwin;
- struct Window* toolwin;
- /* Global record of title bar height */
- int offtop;
-
- /* Need to give prototypes for our functions */
- void handleIDCMP();
- void setupWindows();
- void createWindows(struct Gadget*, struct Screen*, struct Menu*);
- int openLibs();
- void closeLibs();
- struct Menu* createMenuStrip(APTR);
- void setFgPen(int);
- void doGadgetUp(struct IntuiMessage*);
- int doMenuPick(struct Window*, struct IntuiMessage*);
-
- /* Some constants for the position and size of our gadget */
- #define MYBUT_LEFT (10)
- #define MYBUT_TOP (5)
- #define MYBUT_WIDTH (80)
- #define MYBUT_HEIGHT (12)
- #define MYBUT_TEXT "Next Pen"
- #define MYBUT_ID (0)
-
- #define MYPAL_LEFT (170)
- #define MYPAL_TOP (1)
- #define MYPAL_WIDTH (109)
- #define MYPAL_HEIGHT (19)
- #define MYPAL_TEXT "Colour:"
- #define MYPAL_ID (1)
- #define MYPAL_DEPTH (4)
-
- /* The top gap required around the gadgets */
- #define MYTOPGAP (21)
-
- /* The initial pen colour */
- #define MYINITPEN (1)
-
- /* The start of the program */
- void main()
- {
- /* Use a different style of opening libraries... */
- if(openLibs())
- {
- /* Now do the real work */
- setupWindows();
- }
- /* Matched call to close libraries */
- closeLibs();
- }
-
- /* Try to open all the libraries -- return TRUE on success */
- int openLibs()
- {
- if((GfxBase = OpenLibrary("graphics.library",37)) == NULL)
- {
- printf("Error: could not open graphics.library\n");
- return FALSE;
- }
- if((IntuitionBase = OpenLibrary("intuition.library",37)) == NULL)
- {
- printf("Error: could not open intuition.library\n");
- return FALSE;
- }
- if((GadToolsBase = OpenLibrary("gadtools.library",37)) == NULL)
- {
- printf("Error: could not open gadtools.library\n");
- return FALSE;
- }
- return TRUE;
- }
-
- /* Close any open library */
- void closeLibs()
- {
- if(GadToolsBase)
- CloseLibrary(GadToolsBase);
- if(IntuitionBase)
- CloseLibrary(IntuitionBase);
- if(GfxBase)
- CloseLibrary(GfxBase);
- }
-
- /* Setup the windows -- do the GadTools stuff */
- void setupWindows()
- {
- struct Screen* scr;
- UWORD pens[] = { ~0 };
- /* Try to open a new screen with 16 colours (four bitplanes deep) */
- if(scr = OpenScreenTags(NULL,
- SA_Depth, 4,
- /* Enable 3D look by specifying SA_Pens */
- SA_Pens, pens,
- SA_Title, "Hello World Painter",
- TAG_DONE))
- {
- APTR vinfo;
- /* Get the visual info so GadTools can render the gadgets nicely */
- if(vinfo = GetVisualInfo(scr, TAG_DONE))
- {
- /* We can initialise glist in its declaration */
- struct Gadget* glist = NULL;
- struct Gadget* gad;
- int offleft;
- struct NewGadget newgad;
- /* Initialised structure declaration: describes 8pt Topaz font */
- struct TextAttr topazFont = { "topaz.font", 8, 0, 0, };
- /* Start a GadTools gadget list */
- gad = CreateContext(&glist);
- /* The offsets of our window borders */
- offleft = scr->WBorLeft;
- offtop = scr->WBorTop + (scr->Font->ta_YSize + 1);
-
- /* Setup our first gadget */
- newgad.ng_TextAttr = &topazFont;
- newgad.ng_VisualInfo = vinfo;
- newgad.ng_LeftEdge = MYBUT_LEFT + offleft;
- newgad.ng_TopEdge = MYBUT_TOP + offtop;
- newgad.ng_Width = MYBUT_WIDTH;
- newgad.ng_Height = MYBUT_HEIGHT;
- newgad.ng_GadgetText = MYBUT_TEXT;
- newgad.ng_GadgetID = MYBUT_ID;
- newgad.ng_Flags = 0;
- /* Now create it and add it to our list */
- gad = CreateGadget(BUTTON_KIND, gad, &newgad, TAG_END);
-
- /* Setup our second gadget */
- /* (We can reuse newgad, and just change the different bits) */
- newgad.ng_LeftEdge = MYPAL_LEFT + offleft;
- newgad.ng_TopEdge = MYPAL_TOP + offtop;
- newgad.ng_Width = MYPAL_WIDTH;
- newgad.ng_Height = MYPAL_HEIGHT;
- newgad.ng_GadgetText = MYPAL_TEXT;
- newgad.ng_GadgetID = MYPAL_ID;
- newgad.ng_Flags = 0;
- /* Now create it and add it to our list */
- if(gad = CreateGadget(PALETTE_KIND, gad, &newgad,
- /* Initially selected pen */
- GTPA_Color, MYINITPEN,
- /* Depth: 2 to the power MYPAL_DEPTH colours */
- GTPA_Depth, MYPAL_DEPTH,
- /* Gadget will indicate selection */
- GTPA_IndicatorWidth, 16,
- TAG_DONE))
- {
- struct Menu* menustrip;
- /* Remember gadget pointer so we can affect it in message handler */
- palgad = gad;
- if(menustrip = createMenuStrip(vinfo))
- /* If succeeded then all gadgets and menus created */
- createWindows(glist, scr, menustrip);
- FreeMenus(menustrip);
- }
- else
- printf("Error: could not create gadget(s)\n");
- /* Free all the gadgets that were created */
- FreeGadgets(glist);
- FreeVisualInfo(vinfo);
- }
- else
- printf("Error: could not get visual info\n");
- CloseScreen(scr);
- }
- else
- printf("Error: could not create screen\n");
- }
-
- /* Create the menu strip, using GadTools menu functions */
- struct Menu* createMenuStrip(APTR vinfo)
- {
- /* The description of our menus */
- struct NewMenu mymenu[] =
- {
- { NM_TITLE, "Project", 0, 0, 0, 0,},
- { NM_ITEM, "Quit", "Q", 0, 0, 0,},
- { NM_TITLE, "Pen", 0, 0, 0, 0,},
- { NM_ITEM, "Next", "N", 0, 0, 0,},
- { NM_ITEM, "Prev", "P", 0, 0, 0,},
- { NM_ITEM, NM_BARLABEL, 0, 0, 0, 0,},
- { NM_ITEM, "Reset", "R", 0, 0, 0,},
- { NM_END, NULL, 0, 0, 0, 0,},
- };
- struct Menu* menustrip;
- if (menustrip = CreateMenus(mymenu, TAG_END))
- {
- if (LayoutMenus(menustrip, vinfo, TAG_END))
- /* Succeeded, so return menu strip */
- return menustrip;
- else
- {
- /* Failed, so must deallocate before returning */
- FreeMenus(menustrip);
- printf("Error: could not layout menus\n");
- }
- }
- else
- printf("Error: could not create menu strip\n");
- /* Failed, so return NULL */
- return NULL;
- }
-
- /* Actually open the window, in the normal way */
- void createWindows(struct Gadget* glist, struct Screen* scr, struct Menu* menustrip)
- {
- /* Open our drawing window */
- if(drawwin = OpenWindowTags(NULL,
- WA_Left, 0,
- WA_Top, 0,
- /* Make the window the same size as the screen */
- WA_Width, scr->Width,
- WA_Height, scr->Height,
- WA_Flags, WFLG_BACKDROP | WFLG_BORDERLESS | WFLG_REPORTMOUSE,
- WA_IDCMP, IDCMP_MOUSEBUTTONS | IDCMP_MOUSEMOVE | IDCMP_MENUPICK,
- WA_CustomScreen, scr,
- TAG_DONE, 0))
- {
- /* Attach menu strip to drawing window */
- if(SetMenuStrip(drawwin, menustrip))
- {
- /* The minimal height of our tool window */
- int h = MYTOPGAP + offtop + scr->WBorBottom;
- /* Open our tool window */
- if(toolwin = OpenWindowTags(NULL,
- WA_Left, 0,
- WA_Top, scr->Height - h,
- /* Make the window sit in the bottom of the screen */
- WA_Width, scr->Width,
- WA_Height, h,
- WA_Flags, WFLG_CLOSEGADGET | WFLG_DRAGBAR,
- WA_IDCMP, IDCMP_CLOSEWINDOW | BUTTONIDCMP | IDCMP_REFRESHWINDOW | IDCMP_MENUPICK,
- WA_Gadgets, glist,
- WA_CustomScreen, scr,
- WA_Title, "Tools",
- TAG_DONE, 0))
- {
- /* Attach menu strip to tool window, as well */
- if(SetMenuStrip(toolwin, menustrip))
- {
- /* Let GadTools refresh its bits of the tool window */
- GT_RefreshWindow(toolwin, NULL);
- /* Now handle messages from both windows */
- handleIDCMP();
- /* Remove menu strip from tool window */
- ClearMenuStrip(toolwin);
- }
- else
- printf("Error: could not attach menus to tool window\n");
- CloseWindow(toolwin);
- }
- else
- printf("Error: could not open tool window\n");
- /* Remove menu strip from drawing window */
- ClearMenuStrip(drawwin);
- }
- else
- printf("Error: could not attach menus to drawing window\n");
- CloseWindow(drawwin);
- }
- else
- printf("Error: could not open drawing window\n");
- }
-
- /* Our message handling code */
- void handleIDCMP()
- {
- char* text = "Hello World!";
- int going = TRUE;
- int drawing = FALSE;
- ULONG drawsig, toolsig, gotsig;
- drawsig = 1 << drawwin->UserPort->mp_SigBit;
- toolsig = 1 << toolwin->UserPort->mp_SigBit;
- setFgPen(MYINITPEN);
- /* Set the drawing mode to draw only the foreground of text, not the background */
- SetDrMd(drawwin->RPort, JAM1);
- while(going)
- {
- struct IntuiMessage* intuimsg;
- /* Wait for messages to arrive */
- gotsig = Wait(drawsig | toolsig);
- /* Messages have arrived: loop through all of them */
- /* Check messages from the drawing window first */
- if(gotsig & drawsig)
- {
- while(intuimsg = GT_GetIMsg(drawwin->UserPort))
- {
- /* Act on this message... */
- switch(intuimsg->Class)
- {
- case IDCMP_MOUSEBUTTONS:
- switch(intuimsg->Code)
- {
- case SELECTDOWN:
- drawing = TRUE;
- break;
- case SELECTUP:
- drawing = FALSE;
- break;
- }
- /* break; omitted so we draw on click, too */
- case IDCMP_MOUSEMOVE:
- if(drawing)
- {
- Move(drawwin->RPort, intuimsg->MouseX, intuimsg->MouseY);
- Text(drawwin->RPort, text, strlen(text));
- }
- break;
- case IDCMP_MENUPICK:
- going = doMenuPick(drawwin, intuimsg);
- break;
- }
- /* Reply when finished with message */
- GT_ReplyIMsg(intuimsg);
- }
- }
- /* Now check messages from the tool window */
- if(gotsig & toolsig)
- {
- while(intuimsg = GT_GetIMsg(toolwin->UserPort))
- {
- /* Act on this message... */
- switch(intuimsg->Class)
- {
- case IDCMP_CLOSEWINDOW:
- going = FALSE;
- break;
- case IDCMP_REFRESHWINDOW:
- /* You *MUST* remember to ask for and handle these refresh messages */
- GT_BeginRefresh(toolwin);
- GT_EndRefresh(toolwin, TRUE);
- break;
- case IDCMP_GADGETUP:
- doGadgetUp(intuimsg);
- break;
- case IDCMP_MENUPICK:
- going = doMenuPick(toolwin, intuimsg);
- break;
- }
- /* Reply when finished with message */
- GT_ReplyIMsg(intuimsg);
- }
- }
- }
- }
-
- /* Set foreground pen of drawing window */
- void setFgPen(int value)
- {
- /* Wrap when reached the end of the palette gadget's colours */
- pen = value % (1<<MYPAL_DEPTH);
- SetAPen(drawwin->RPort, pen);
- /* If the palette gadget has been made, update it with new pen value */
- if(palgad)
- GT_SetGadgetAttrs(palgad, toolwin, NULL, GTPA_Color, pen, TAG_DONE);
- }
-
- /* Process IDCMP_GADGETUP event */
- void doGadgetUp(struct IntuiMessage* intuimsg)
- {
- struct Gadget* gad = (struct Gadget*)(intuimsg->IAddress);
- switch(gad->GadgetID)
- {
- case MYBUT_ID:
- /* Our button was clicked! Set foreground to next pen colour */
- setFgPen(pen+1);
- break;
- case MYPAL_ID:
- /* Our palette gadget was clicked! Set foreground to gadget colour */
- setFgPen(intuimsg->Code);
- break;
- }
- }
-
- /* Process IDCMP_MENUPICK event */
- int doMenuPick(struct Window* win, struct IntuiMessage* intuimsg)
- {
- UWORD menuCode, menuNumber, itemNumber;
- /* Loop over all the menu selections in the menu code */
- for(menuCode = intuimsg->Code;
- menuCode != MENUNULL;
- menuCode = ItemAddress(win->MenuStrip, menuCode)->NextSelect)
- {
- /* Extract the menu number and menu item number from the menu code */
- menuNumber = MENUNUM(menuCode);
- itemNumber = ITEMNUM(menuCode);
- /* Now decide what to do based on what menu item was selected */
- switch(menuNumber)
- {
- case 0: /* Project menu */
- /* Only one item: Quit */
- if(itemNumber == 0)
- return FALSE; /* Stop going */
- break;
- case 1: /* Pen menu */
- switch(itemNumber)
- {
- case 0: /* Next */
- setFgPen(pen+1);
- break;
- case 1: /* Prev */
- setFgPen(pen-1);
- break;
- case 3: /* Reset (item 2 is the bar!) */
- setFgPen(MYINITPEN);
- break;
- }
- break;
- }
- }
- /* Keep going */
- return TRUE;
- }
-